package Lono::Recipe::Builder;

use warnings;
use strict;

our $VERSION = '0.0.1';

=head1 NAME

Lono::Recipe::Builder - Safely creates Lono::Recipe objects.

=head1 SYNOPSIS

use Lono::Recipe::Builder;

my $builder = Lono::Recipe::Builder->new;
$builder->set_name( 'Burrito' );

=cut

use base qw( Class::Accessor );

__PACKAGE__->follow_best_practice;

use Lono::Recipe;
use Lono::Parser::Ingredient;
use Lono::Recipe::Ingredient;

# Automatically create setters for all fields currently available in the
# nice class we're going to make an instance of
Lono::Recipe::Builder->mk_accessors( Lono::Recipe->recipe_fields );

my @REQUIRED_FIELDS = qw( name ingredients preparation );

sub add_ingredient {
    my ( $self, $ingredient ) = @_;

    # TODO: incorporate ingredient parser
    my $ing = Lono::Recipe::Ingredient->new;
    $ing->set_raw_text( $ingredient );
    push( @{$self->{'ingredients'}}, $ing );
}

# We should override the default behavior 

sub has_valid_recipe {
    # TODO: implement real recipe validation alogrithm
    return 1;
}

sub build_recipe {
    my $self = shift;
    my $recipe = Lono::Recipe->new;

    # TODO: have this use Lono::Recipe->recipe_fields instead of 
    # hard-coding.
    $recipe->set_name( $self->{'name'} );
    $recipe->set_ingredients( $self->{'ingredients'} );
    $recipe->set_preparation( $self->{'preparation'} );
    $recipe->set_description( $self->{'description'} );
    $recipe->set_yield_amount( $self->{'yield_amount'} );
    $recipe->set_yield_unit( $self->{'yield_unit'} );
    $recipe->set_active_time( $self->{'active_time'} );
    $recipe->set_total_time( $self->{'total_time'} );
    $recipe->set_nutrition( $self->{'nutrition'} ); 
    $recipe->set_source( $self->{'source'} );
    $recipe->set_url( $self->{'url'} );
    
    return $recipe;
}

1;
